package controllers

import (
	"blog-server/app/common/request"
	"blog-server/app/common/response"
	"blog-server/app/services"
	"blog-server/global"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type backendControllers struct {
}

var BackendController = new(backendControllers)

// 新增文章
func (backendControllers *backendControllers) CreateShare(c *gin.Context) {
	var form request.ShareModel
	if err := c.ShouldBindJSON(&form); err != nil {
		global.App.Log.Error("参数错误", zap.Any("err", err))
		response.ValidateFail(c, "参数错误")
		return
	}
	errs := services.ShareService.InsetShare(form)
	if errs != nil {
		global.App.Log.Error("创建分享失败", zap.Any("err", errs))
		response.BusinessFail(c, "系统错误")
		return
	}
	response.Success(c, "创建成功")
}

// UpdateShare 更新文章
func (backendControllers *backendControllers) UpdateShare(c *gin.Context) {
	var form request.ShareModel
	if err := c.ShouldBindJSON(&form); err != nil {
		global.App.Log.Error("参数错误", zap.Any("err", err))
		response.ValidateFail(c, "参数错误")
		return
	}
	if errs := services.ShareService.UpdateShare(form); errs != nil {
		global.App.Log.Error("更新分享失败", zap.Any("err", errs))
		response.BusinessFail(c, "系统错误")
		return
	}
	response.Success(c, "更新成功")
}

// 获取文章列表
func (backendControllers *backendControllers) GetShareList(c *gin.Context) {
	var form request.ShareList
	if err := c.ShouldBindJSON(&form); err != nil {
		global.App.Log.Error("参数错误", zap.Any("err", err))
		response.ValidateFail(c, "参数错误")
		return
	}
	list, err := services.ShareService.QueryShareList(form)
	if err != nil {
		global.App.Log.Error("获取分享列表失败", zap.Any("err", err))
		response.BusinessFail(c, "系统错误")
		return
	}
	response.Success(c, list)
}

// 获取文章详情
func (backendControllers *backendControllers) GetShareDetail(c *gin.Context) {
	var form request.ShareDetail
	if err := c.ShouldBindJSON(&form); err != nil {
		global.App.Log.Error("参数错误", zap.Any("err", err))
		response.ValidateFail(c, "参数错误")
		return
	}
	if detail, err := services.ShareService.QueryShareDetail(form); err != nil {
		global.App.Log.Error("获取分享详情失败", zap.Any("err", err))
		response.BusinessFail(c, "系统错误")
		return
	} else {
		response.Success(c, detail)
	}
}

// 上下架文章
func (backendControllers *backendControllers) UpdateShareStatus(c *gin.Context) {
	var form request.ShareSelf
	if err := c.ShouldBindJSON(&form); err != nil {
		global.App.Log.Error("参数错误", zap.Any("err", err))
		response.ValidateFail(c, "参数错误")
		return
	}
	if errs := services.ShareService.UpdateShareStatus(form); errs != nil {
		global.App.Log.Error("更新分享状态失败", zap.Any("err", errs))
		response.BusinessFail(c, "系统错误")
		return
	}
	response.Success(c, "更新成功")
}

// 删除文章
func (backendControllers *backendControllers) DeleteShare(c *gin.Context) {
	var form request.ShareSelf
	if err := c.ShouldBindJSON(&form); err != nil {
		global.App.Log.Error("参数错误", zap.Any("err", err))
		response.ValidateFail(c, "参数错误")
		return
	}
	if errs := services.ShareService.DeleteShare(form); errs != nil {
		global.App.Log.Error("删除分享失败", zap.Any("err", errs))
		response.ValidateFail(c, "系统错误")
	}
	response.Success(c, "删除成功")
}
